package com.atguigu.yygh.user.service.impl;

import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.common.handler.YyghException;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.mapper.PatientMapper;
import com.atguigu.yygh.user.service.PatientService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author 杨林
 * @create 2022-11-15 15:11 星期二
 * description:
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper,Patient> implements PatientService {

    @Autowired
    private DictFeignClient dictFeignClient;

    //根据用户id查询就诊人列表
    @Override
    public List<Patient> findListByUserId(Long userId) {
        if (StringUtils.isEmpty(userId)){
            throw new YyghException(20001,"用户id不能为空");
        }
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<Patient> patientList = baseMapper.selectList(queryWrapper);
        for (Patient patient : patientList) {
            this.packPatient(patient);
        }
        return patientList;
    }

    private void packPatient(Patient patient) {
        //就诊人证件类型
        String certificatesType = patient.getCertificatesType();
        //联系人证件类型
        String contactsCertificatesType = patient.getContactsCertificatesType();
        //省
        String provinceCode = patient.getProvinceCode();
        //市
        String cityCode = patient.getCityCode();
        //区
        String districtCode = patient.getDistrictCode();
        String certificatesTypeString = dictFeignClient.getName(certificatesType);
        String contactsCertificatesTypeString = dictFeignClient.getName(contactsCertificatesType);
        String provinceString = dictFeignClient.getName(provinceCode);
        String cityString = dictFeignClient.getName(cityCode);
        String districtString = dictFeignClient.getName(districtCode);

        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);

        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);

        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());
    }

    //根据就诊人id查询就诊人
    @Override
    public Patient getPatientById(Long id) {

        if (StringUtils.isEmpty(id)){
            throw new YyghException(20001, "id不能为空");
        }

        Patient patient = baseMapper.selectById(id);
        this.packPatient(patient);
        return patient;
    }
}
